<?php
 global $search_term2;

$q="";//$_REQUEST[q];
$hl="";//$_REQUEST[hl];
$ie="";//$_REQUEST[ie];
$start="";//$_REQUEST[start];
$sa="";//$_REQUEST[sa];
$ss="";
$search_term2 = $_REQUEST['ss'];
	$output ="";
	$output2 ="";
	$a="";
	$b="";

$q="site:happy.co.th+".urlencode($_REQUEST['ss']);
$hl=$_REQUEST['hl'];
$ie=$_REQUEST['ie'];
$start=$_REQUEST['start'];
$sa=$_REQUEST['sa'];
$ss=$_REQUEST['sa'];

	$url_to_scrape = "http://www.google.co.th/search?q=$q&hl=$hl&ie=$ie&start=$start&sa=$sa";
	//echo $url_to_scrape."<BR>";
	$page_content = file_get_contents($url_to_scrape);
	$pageging = $page_content ;
	//echo $page_content;
	
	//regular expression to parse the page_content	
	//$regex = "/<h2 class=r><a href=\"(.*?)\" class=l>(.*?)\<\/a>/";
	$regex = "/<div class=g>(.*?)<\/div>/";
	$regex2 = "/<div id=navbar class=n>(.*?)<\/div>/";
	
	preg_match_all($regex, $page_content, $matches);
	preg_match_all($regex2, $pageging, $matches2);
	
	foreach ($matches[1] as $key => $result) {
		$b = preg_replace("/<h2 (.*?)>/i","<BR>",$result);
		$b = preg_replace("/<\/h2>/i","",$b);
		$b = preg_replace("/<nobr><a class=fl(.*?)<\/nobr>/i","",$b);

		//$b = preg_replace("/Cached/i","<h4>",$b);
		//$b = preg_replace("/Similar pages/i","<h4>",$b);
		$output .= $b;
	}
	foreach ($matches2[1] as $key => $result2) {
		$a =preg_replace("/<img src=(.*?)><br>/i","",$result2);
		$a =preg_replace("/cellpadding=0/i","cellpadding=3",$a);
		$a =preg_replace("/\/search/i","./nextpage.php",$a);
		$a =preg_replace("/q=/i","ss=$search_term2&q=",$a);
		$output2 .= $a ;
	}
	//echo "<p>".htmlspecialchars($output2)."</p>";	

?>